<?php

namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;//缓存
use Illuminate\Support\Facades\Gate;

class UploadController extends Controller{
    public function upload(){
        $imgArr = [];
        list($key,$fileInfo) = each($_FILES);
        $tmp = explode("/", $fileInfo['type']);
        $type = $tmp[1];//获取文件的后缀名
        $imgList = array("jpeg","JPEG","PNG","png","jpg","JPG");
        if (in_array($type, $imgList)){
            if (is_uploaded_file($fileInfo['tmp_name']))
            {
                $year = date('Y',time());
                $fileName = time().'.'.$type;//
                $dir =  public_path('upload'.DIRECTORY_SEPARATOR.$year.DIRECTORY_SEPARATOR);
                if (!is_dir($dir)){
                    mkdir($dir,'0777');
                }
                $bool = move_uploaded_file($fileInfo['tmp_name'], $dir.$fileName);
                $imgURL = DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.$year.DIRECTORY_SEPARATOR.$fileName;
                if ($bool)
                {
                    $imgArr['status'] = 1; //上传成功
                    $imgArr['imgURL'] = $imgURL;
                }else{
                    $imgArr['status'] = -1;//上传失败
                }
            }
        }else{
            $imgArr['status'] = -10;//文件上传格式错误
        }

        echo json_encode($imgArr);
    }
}
